import datetime
from pymongo import mongo_client
import requests
from requests.adapters import HTTPAdapter
from urllib3.exceptions import InsecureRequestWarning
import redis

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/86.0.4240.111 Safari/537.36 ',
}
cookies = [
    {'Cookie': 'SINAGLOBAL=5292419599023.839.1632041533733; ALF=1665133334; SCF=AipZi3nwxW-Rlk_9gqzVTyGoAJ_Iivrfms5dfn4-_8WP5tXiVGsHQo3qj5y8Qjtwi3I6oUOqO--uZ3ELZtuRKak.; wvr=6; _s_tentry=login.sina.com.cn; UOR=,,login.sina.com.cn; Apache=4651444063936.137.1633597338628; ULV=1633597338633:3:1:1:4651444063936.137.1633597338628:1632381437030; SSOLoginState=1633942408; SUB=_2A25MZ4vYDeRhGeNK41AX-CjJyzWIHXVvqxWQrDV8PUJbkNAKLVPlkW1NSQ8yzjsDZ5xRxc0oZ17MdwYogq1PKoI8; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WWF61p6iuMrp-1Un-sSGLhO5NHD95QfShnESoncSK54Ws4Dqcjki--Xi-i2iK.fi--ciK.ciKyhi--ciK.ciKnNP0M4eBtt; webim_unReadCount=%7B%22time%22%3A1634088024043%2C%22dm_pub_total%22%3A3%2C%22chat_group_client%22%3A0%2C%22chat_group_notice%22%3A0%2C%22allcountNum%22%3A14%2C%22msgbox%22%3A0%7D'}
]
request = requests.session()
request.mount('http://', HTTPAdapter(max_retries=3))
request.mount('https://', HTTPAdapter(max_retries=3))


def redis_connect():
    host = "127.0.0.1"
    port = '6379'
    r = redis.StrictRedis(host=host, port=port, db=1, password=123456)
    return r


def insertMongo(dict, table):
    """插入mongodb数据库表格信息"""
    client = mongo_client("mongodb://192.168.0.10:27017/")
    collection = client['weibo'][table]
    collection.insert_one(dict)

# uid 用户id
# 关注  获取关注者的id 被关注者的id  爬取时间
# https://m.weibo.cn/api/container/getIndex?containerid=231051_-_fans_-_1651428902
# https://m.weibo.cn/api/container/getIndex?containerid=231051_-_fans_-_2615577972
# https://m.weibo.cn/api/container/getIndex?containerid=231051_-_fans_-_1651428902&since_id=22
def attention(uid):
    url = 'https://m.weibo.cn/api/container/getIndex?containerid=231051_-_fans_-_{}'.format(
        uid)
    print(url)
    res = request.get(url, headers=headers)
    print(len(res.json()['data']['cards']))
    if len(res.json()['data']['cards']) == 0:
        res = request.get(url, headers=headers, timeout=10)
    if len(res.json()['data']['cards']) == 1:
        for data in res.json()['data']['cards'][0]['card_group']:
            dict1 = {}
            da = datetime.datetime.strptime(
                str(datetime.date.today()), '%Y-%m-%d')
            todays = datetime.datetime.strftime(da, '%Y-%m-%d')
            try:
                dict1['关注者id'] = data['user']['id']
            except:
                continue
            dict1['被关注者id'] = uid
            dict1['抓取时间'] = todays
            insertMongo(dict1, 'weibo_attention')
            print(
                '==================================插入数据成功======================================')
    else:
        for data in res.json()['data']['cards'][-1]['card_group']:
            dict1 = {}
            da = datetime.datetime.strptime(
                str(datetime.date.today()), '%Y-%m-%d')
            todays = datetime.datetime.strftime(da, '%Y-%m-%d')
            dict1['关注者id'] = data['user']['id']
            dict1['被关注者id'] = uid
            dict1['抓取时间'] = todays
            insertMongo(dict1, 'weibo_attention')
            print(
                '==================================插入数据成功======================================')
    try:
        since_id = res.json()['data']['cardlistInfo']['since_id']
    except:
        return
    try:
        while True:
            url = 'https://m.weibo.cn/api/container/getIndex?containerid=231051_-_fans_-_{}&since_id={}'.format(
                uid, since_id)
            res = request.get(url, headers=headers)
            since_id = res.json()['data']['cardlistInfo']['since_id']
            for data in res.json()['data']['cards'][0]['card_group']:
                dict1 = {}
                da = datetime.datetime.strptime(
                    str(datetime.date.today()), '%Y-%m-%d')
                todays = datetime.datetime.strftime(da, '%Y-%m-%d')
                dict1['关注者id'] = data['user']['id']
                dict1['被关注者id'] = uid
                dict1['抓取时间'] = todays
                print(
                    '==================================插入数据成功======================================')
                insertMongo(dict1, 'weibo_attention')
    except Exception as e:
        print('报错了，原因是', e)
        return


if __name__ == "__main__":
    r = redis_connect()
    uids = r.smembers('uid')
    p = r.pubsub()
    p.subscribe('uid')
    print('开始监听:')
    for item in p.listen():
        if item['type'] == 'message':
            uid = item['data'].decode()
            if r.exists(uid) == 1:
                continue
            r.set(uid,uid)
            attention(uid)
    

